<?php

class App_Helper_Table {
   
   public $rows;
   private $attribs = array();
   
   public function __construct($rows=array(), $attribs=array()) {
      $this->rows = $rows;
      $this->attribs = $attribs;
   }
   
   public function addRow($cells=array(), $attribs=array(), $rowid="", $isHeader=false) {
      $row = new App_Helper_Table_Row($cells, $attribs, $rowid, $isHeader);
      $this->addRowObject($row, $rowid);
   }
   
   public function addRowObject($row) {
      $rows = array();
      if(!is_array($row)) {
         $rows = array($row);
      } else {
         $rows = $row;
      }
      foreach($rows as $row) {
         if($row->rowid) {
            $this->rows[$row->rowid] = $row;
         } else {
            $this->rows[] = $row;
         }
      }
   }
   
   public function addCell($data, $attribs=array(), $rowid="", $cellid="") {
      $cell = new App_Helper_Table_Cell($data, $attribs);
      if($rowid && isset($this->rows[$rowid])) {
         if(isset($cellid)) {
            $this->rows[$rowid] = $cell;
         } else {
            $this->rows[] = $cell;
         }
      }
   }
   
   public function __toString() {
      $str = "<table";
      foreach($this->attribs as $key => $val)  {
         $str .= " $key=\"$val\"";
      }
      $str .= ">";
      foreach($this->rows as $row) {
         $str .= $row;
      }
      $str .= "</table>\r\n";
      return $str;
   }
   
}



